<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Product extends CI_Controller {

    //Init $layout_data
    private static $layout_data = array();

    //Init $product_data
    private static $product_data = array();

    /**
     * construct function
     */
    public function __construct()
    {
        parent::__construct();

        //Assign to layouts
        $this->layout_data['pageTitle'] = 'Product - My site - Backend - Mr.Alex';
        $this->layout_data['navTab'] = 'product';

        //Init product model
        $this->load->model('product_model', 'productModel');
    }

    /**
     * Index function
     */
    public function index()
    {
        //Get page
        $page = $this->uri->segment(3)?$this->uri->segment(3):1;

        //Set limit, offset
        $limit = 20;
        $offset = ($page-1)*$limit;

        //Get categoryId
        $categoryId = $this->input->post('CategoryID')?$this->input->post('CategoryID'):NULL;

        //Get list product
        $this->product_data['productList'] = $this->productModel->getListProduct($limit, $offset, $total, $categoryId);

        //Set layouts content data
        $this->layout_data['content'] = $this->load->view('product/index', $this->product_data, TRUE);

        //Render layouts
        $this->load->view('layouts/main', $this->layout_data);
    }

    /**
     * Add product function
     */
    public function add()
    {
        if($this->input->post('submit'))
        {
            //Upload images

            //Get params
            $params = array(
                'CategoryID'    =>  $this->input->post('CategoryID'),
                'Code'          =>  $this->input->post('Code'),
                'Name'          =>  $this->input->post('Name'),
                'Description'   =>  $this->input->post('Description'),
                'Price'         =>  $this->input->post('Price'),
                'PriceSale'     =>  $this->input->post('PriceSale'),
                'Image'         =>  '',
                'Status'        =>  $this->input->post('Status'),
                'CreateDate'    =>  time(),
            );

            //insert to DB
            $result = $this->productModel->insertProduct($params);

            //Check result
            if($result)
            {
                $this->product_data['productSuccess'] = 'Sản phẩm '.$this->input->post('Name').' đã được thêm thành công';
            }
            else
            {
                $this->product_data['productError'] = 'Có lỗi xảy ra, vui lòng thử lại';
            }
        }

        //Load category model
        $this->load->model('category_model', 'categoryModel');
        
        //Get category List
        $this->product_data['categoryList'] = $this->categoryModel->getCategoryList();

        //Set layouts content data
        $this->layout_data['content'] = $this->load->view('product/add', $this->product_data, TRUE);

        //Render layouts
        $this->load->view('layouts/main', $this->layout_data);
    }

    /**
     * Update product function
     */
    public function update()
    {
        if($this->input->post('submit'))
        {
            //Check title NOT null
            if($this->input->post('Name'))
            {
                //Get params
                $params = array(
                    'ParentID'  =>  $this->input->post('ParentID'),
                    'Type'  =>  $this->input->post('Type'),
                    'Name'  =>  $this->input->post('Name'),
                    'Description'  =>  $this->input->post('Description'),
                    'Status'  =>  $this->input->post('Status')
                );

                //Get productId
                $productId = $this->uri->segment(4);

                //Check productId
                if($productId)
                {
                    //Update to DB
                    $this->productModel->editproduct($productId, $params);

                    //Redirect index
                    redirect('product');
                }
                else
                {
                    $this->product_data['productError'] = 'Không tìm thấy product';
                }
            }
            else
            {
                $this->product_data['productError'] = 'Vui lòng điền tên product';
            }
        }

        //Get list product root
        $this->product_data['productRoot'] = $this->productModel->getproductRoot();

        //Get productId
        $productId = $this->uri->segment(4);

        //Check productId
        if($productId)
        {
            //Get productDetail
            $this->product_data['productDetail'] = $this->productModel->getproductDetail($productId);
        }

        //Set layouts content data
        $this->layout_data['content'] = $this->load->view('product/update', $this->product_data, TRUE);

        //Render layouts
        $this->load->view('layouts/main', $this->layout_data);
    }

    /**
     * Remove product function
     */
    public function remove()
    {
        //Get productId
        $productId = $this->uri->segment(4);

        //Check productId
        if($productId)
        {
            //Remove product
            $this->productModel->deleteproduct($productId);

            redirect('product');
        }
    }

}

/* End of file Home.php */
/* Location: ./application/controllers/welcome.php */